package gogogo.liwushuo.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

import gogogo.liwushuo.R;
import gogogo.liwushuo.bean.SingleProductBean;

/**
 * Created by cong on 2016/7/20.
 */
public class SpLeftRvAdapter extends RecyclerView.Adapter<SpLeftRvAdapter.MyHolder> {
    private Context mContext;
    private List<SingleProductBean.DataBean.CategoriesBean> mList;
    private OnItemClickLisener myLisener;
    private View mView;

    public SpLeftRvAdapter(Context context, List<SingleProductBean.DataBean.CategoriesBean> list, View firstView) {
        mContext = context;
        mList = list;
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, final int viewType) {
        final View view = LayoutInflater.from(mContext).inflate(R.layout.item_singleproduct_left, parent, false);
        if (viewType == 0) {
            view.setBackgroundColor(mContext.getResources().getColor(R.color.colorWhite));
            view.findViewById(R.id.sp_left_ll).setVisibility(View.VISIBLE);
            ((TextView) view.findViewById(R.id.sp_left_tv)).setTextColor(mContext.getResources().getColor(R.color.colorToolbar));
            mView = view;
        }
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mView != null) {
                    mView.setBackgroundColor(mContext.getResources().getColor(R.color.categorySearchColor));
                    mView.findViewById(R.id.sp_left_ll).setVisibility(View.INVISIBLE);
                    ((TextView) mView.findViewById(R.id.sp_left_tv)).setTextColor(mContext.getResources().getColor(R.color.colorBlack));
                    mView = null;
                }
                myLisener.onItemClickListener(view, viewType);
            }
        });
        return new MyHolder(view);
    }

    @Override
    public void onBindViewHolder(MyHolder holder, final int position) {
        holder.tv.setText(mList.get(position).getName());
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    public interface OnItemClickLisener {
        void onItemClickListener(View view, int position);
    }

    public void setOnItemClickLisener(OnItemClickLisener lisener) {
        myLisener = lisener;
    }

    class MyHolder extends RecyclerView.ViewHolder {
        private TextView tv;
        private LinearLayout ll;

        public MyHolder(View itemView) {
            super(itemView);
            tv = (TextView) itemView.findViewById(R.id.sp_left_tv);
            ll = (LinearLayout) itemView.findViewById(R.id.sp_left_ll);
        }
    }
}
